<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>新建固定资产</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-first">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>批量添加</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>资产类别</th>
                            <th>资产编号</th>
                            <th><text>*</text>资产名称</th>
                            <th>规格型号</th>
                            <th>品牌</th>
                            <th><text>*</text>单位</th>
                            <th>采购日期</th>
                            <th>采购金额(元)</th>
                            <th><text>*</text>管理部门</th>
                            <th>存放位置</th>
                            <th>备注</th>
                            <th>附件</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>


    function choose_deptid(obj){
        var index = $(obj).parent().parent().find('td:first-child').text();
        openPage('../public/choose_userpart.php?dom=deptid&type=dept&index='+index+'&page=asset','70%','70%');
    }

    var pub_assettype
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_asset_add}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;

                pub_assettype = res.data.assettype

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<div class="assettype"></div>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input number22"></td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input gui"></td>';
                listAddStr += '<td><input type="text" class="layui-input pin"></td>';
                listAddStr += '<td><input type="text" class="layui-input danwei"></td>';
                listAddStr += '<td><input type="text" class="layui-input caidt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="number" class="layui-input money"></td>';
                listAddStr += '<td><input type="text" class="layui-input deptid" readonly onclick="choose_deptid(this)"></td>';
                listAddStr += '<td><input type="text" class="layui-input cunaddr"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '<td>'+public_show_file_deta('list_list', [],1)+'</td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1, {name:'assettype', data:res.data.assettype, value:''});

                render_date();
                render_from();
            }
        });

        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();

            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var assettype = $(this).find('.assettype').find('input').val();
                    var number = $(this).find('.number22').val();
                    var name = $(this).find('.name').val();
                    var gui = $(this).find('.gui').val();
                    var pin = $(this).find('.pin').val();
                    var danwei = $(this).find('.danwei').val();
                    var caidt = $(this).find('.caidt').val();
                    var money = $(this).find('.money').val();
                    var deptid = $('.deptid').eq(i).attr('key');
                    var cunaddr = $(this).find('.cunaddr').val();
                    var explain = $(this).find('.explain').val();
                    var files = public_deta_file_value(this);

                    var index = i + 1;
                    if(!assettype){
                        throw new Error('第'+index+'行，请选择资产类别');
                    }
                    if(!name){
                        throw new Error('第'+index+'行，请输入资产名称');
                    }
                    if(!danwei){
                        throw new Error('第'+index+'行，请输入单位');
                    }
                    if(!deptid){
                        throw new Error('第'+index+'行，请选择管理部门');
                    }

                    list.push({
                        assettype:assettype,
                        number:number,
                        name:name,
                        gui:gui,
                        pin:pin,
                        danwei:danwei,
                        caidt:caidt,
                        money:money,
                        deptid:deptid,
                        cunaddr:cunaddr,
                        explain:explain,
                        files:files,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;



            facade.ajax({url: ask_asset_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });







    var ctrlv_field_name = [
        {field: 'number22', name:'资产编号'},
        {field: 'name', name:'资产名称'},
        {field: 'gui', name:'规格型号'},
        {field: 'pin', name:'品牌'},
        {field: 'danwei', name:'单位'},
        {field: 'caidt', name:'采购日期',type:'date'},
        {field: 'money', name:'采购金额(元)',type:'number'},
        {field: 'cunaddr', name:'存放位置'},
        {field: 'explain', name:'备注'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td>';
                str += '<div class="assettype assettype'+nums+'" key="'+nums+'"></div>';
                str += '</td>';
                str += '<td><input type="text" class="layui-input number22" value="'+list[key].number22+'"></td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><input type="text" class="layui-input gui" value="'+list[key].gui+'"></td>';
                str += '<td><input type="text" class="layui-input pin" value="'+list[key].pin+'"></td>';
                str += '<td><input type="text" class="layui-input danwei" value="'+list[key].danwei+'"></td>';
                str += '<td><input type="text" class="layui-input caidt bdate_ymd" value="'+list[key].caidt+'"></td>';
                str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                str += '<td><input type="text" class="layui-input deptid" readonly onclick="choose_deptid(this)"></td>';
                str += '<td><input type="text" class="layui-input cunaddr" value="'+list[key].cunaddr+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '<td>'+public_show_file_deta('list_list', [],1)+'</td>';
                str += '</tr>';
                $("#list_list").append(str);
                xmselect_list_dan('assettype'+nums, pub_assettype, '');
            }
        }
        render_date();
        render_from();
    }

</script>
</body>
</html>
